package lanqiao;

import java.util.ArrayList;
import java.util.List;

public class NQueen {
    public static ArrayList<ArrayList<Integer>> arr = new ArrayList<>();
    public static void main(String[] args) {
        List<Integer> queen = new ArrayList<>(); // create a list to store the positions of queens
        play(queen); // enter recursion
        for (int i = 0; i < arr.size(); i++) {
            System.out.print("res"+(i+1)+": ");
            for (Integer integer : arr.get(i)) {
                System.out.print(integer + 1 + " ");
            }
            System.out.println();
        }
    }

    public static int play(List<Integer> v) {
        int sum = 0;

        for (int i = 0; i < 8; i++) { // per piece
            if (available(v, i) && v.size() < 7) {
                v.add(i);
                sum += play(v);
                v.remove(v.size() - 1);
            }
            if (available(v, i) && v.size() == 7) {
                sum += 1;
                v.add(i);
                myshow(v);
                v.remove(v.size() - 1);
            }
        }
        return sum;
    }

    public static boolean available(List<Integer> v, int k) { // if position k is available for chess
        int pos = v.size();
        for (int i = 0; i < v.size(); i++)
            if (v.get(i) == k || (v.get(i) + i) == (k + pos) || (i - v.get(i)) == (pos - k))
                return false;

        return true;
    }

    public static void myshow(List<Integer> v) {
        arr.add(new ArrayList<>());
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (v.get(i) == j)
                    arr.get(arr.size() - 1).add(j);
            }
        }
    }
}